<?php

namespace app\common\model\user;

use think\Model;


class Withdraw extends Model
{

    

    

    // 表名
    protected $name = 'self_withdraw';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'type_text',
        'status_text',
        'createtime_text',
        'finishtime_text',
    ];
    
    protected static function init()
    {
        self::beforeInsert(function ($row) {
            $money = isset($row['money']) ? $row['money'] : 0;
            $rate = config("site.withdraw_service");
            $row['service_charge'] = 0;
            if($rate){
                $row['service_charge'] = bcmul($money, $rate /100, 2);
            }
            $row['real_amount'] = bcsub($money, $row['service_charge'], 2);
            return $row;
        });
    }
    
    public function getTypeList()
    {
        return ['0' => __('Type 0'), '1' => __('Type 1'), '2' => __('Type 2')];
    }

    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2')];
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getFinishtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['finishtime']) ? $data['finishtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getCreatetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['createtime']) ? $data['createtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setFinishtimeAttr($value)
    {
        return !$value ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }
 

}
